package SRV;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Negocio.Direccion;

public class GoogleSRV {
	
public double calcularDistancia (Direccion dir1, Direccion dir2) throws IOException, JSONException, URISyntaxException{
		
		String API_KEY="AIzaSyD-pLgQUlHeqWEUlmGznSBRODxK6Fixw40";
		String origen = "/maps/api/directions/json?origin="+URLEncoder.encode(dir1.getCalle())+","+URLEncoder.encode(dir1.getLocalidad())+","+URLEncoder.encode(dir1.getProvincia())+","+URLEncoder.encode(dir1.getPais());
		String destino = "&destination="+URLEncoder.encode(dir2.getCalle())+","+URLEncoder.encode(dir2.getLocalidad())+","+URLEncoder.encode(dir2.getProvincia())+","+URLEncoder.encode(dir2.getPais());
		String opt = "&key="+API_KEY;
		
		String urltotal = "https://" + "maps.googleapis.com" + origen + destino + opt;
				
		//leo la url
		JSONObject obj = readJsonFromUrl(urltotal);
		
		//bajo el array de routes
		JSONObject routes = obj.getJSONArray("routes").getJSONObject(0);
	    //bajo el array de legs
		JSONObject legs = routes.getJSONArray("legs").getJSONObject(0);
	    //bajo objeto distance
		JSONObject distance = legs.getJSONObject("distance");
	    

		return distance.getDouble("value")/1000;
	}
	
	private static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
	    //InputStream is = url.openStream();
	    InputStream is = new URL(url).openStream();
	    try {
	    	BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
	    	String jsonText = readAll(rd);
	    	JSONObject json = new JSONObject(jsonText);
	    	return json;
	    } finally {
	    	is.close();
	    }
	}
  
	private static String readAll(Reader rd) throws IOException {
	    StringBuilder sb = new StringBuilder();
	    int cp;
	    while ((cp = rd.read()) != -1) {
	    	sb.append((char) cp);
	    }
	    return sb.toString();
	}
	
public void calcularNavegacion(Direccion dir1, Direccion dir2) throws IOException, JSONException, URISyntaxException{
		
		//ARMO LA URL con los parametros mas la key
		String API_KEY="AIzaSyD-pLgQUlHeqWEUlmGznSBRODxK6Fixw40";
		String origen = "/maps/api/directions/json?origin="+URLEncoder.encode(dir1.getCalle())+","+URLEncoder.encode(dir1.getLocalidad())+","+URLEncoder.encode(dir1.getProvincia())+","+URLEncoder.encode(dir1.getPais());
		String destino = "&destination="+URLEncoder.encode(dir2.getCalle())+","+URLEncoder.encode(dir2.getLocalidad())+","+URLEncoder.encode(dir2.getProvincia())+","+URLEncoder.encode(dir2.getPais());
		String opt = "&key="+API_KEY;
		
		String urltotal = "https://" + "maps.googleapis.com" + origen + destino + opt;
		System.out.println(urltotal);
						
		//leo la url y cargo el JSON Object q devueve google en obj 
		JSONObject obj = readJsonFromUrl(urltotal);
		
		//bajo el array de routes
		JSONObject routes = obj.getJSONArray("routes").getJSONObject(0);
	    //bajo el array de legs
		JSONObject legs = routes.getJSONArray("legs").getJSONObject(0);
	    //bajo objeto distance
		JSONObject distance = legs.getJSONObject("distance");
		
		//cargo el array de pasos
		JSONArray steps = legs.getJSONArray("steps");
	    for (int i=0; i<steps.length(); i++){
			JSONObject distance2 = steps.getJSONObject(i).getJSONObject("distance");
			System.out.println(distance2.getString("text"));
			String html_instructions = steps.getJSONObject(i).getString("html_instructions");
			html_instructions = html_instructions.replaceAll("\\<.*?>", "");
			System.out.println(html_instructions);
		}

		//test
		//System.out.println(distance.getString("text"));
		System.out.println("Distancia Total: " + distance.getDouble("value")/1000 + "Kms");
		
	    //devuelvo la distancia
	//	return distance.getDouble("value")/1000;
	}

}
